Repository navigation
Index color, piece and castling-side tables by their enums - #67
Merged
Merged
Conversation
Move generation wrote through PseudoLegalMove*& cursors into caller-owned std::array buffers, and legal moves came back in a heap-allocated std::vector. Add InplaceVector<T, N>, a fixed-capacity vector with the subset of the std::inplace_vector interface the engine needs. std::inplace_vector itself needs libstdc++ 16, and GCC 16 currently builds this engine slower. GeneratePseudoLegalMoves and GenerateLegalMoves return PseudoLegalMoves and LegalMoves by value, the helpers append with unchecked_push_back, the en passant filter is erase_if, and callers use range-for, std::ranges::contains and views::concat instead of pointer loops. GenerateMoves duplicated GenerateLegalMoves and is gone. The principal variation gets its own type bounded by the deepest depth, so move generation and PV extraction no longer allocate. Search is unchanged: identical nodes and best moves on 30 positions, perft startpos d5 4865609 and kiwipete d4 4085603. The push loops keep the size in a register and store it once, and the returned lists are built in place without copies. Instructions per node +0.04% (11708-11719 vs 11714-11721), cycles within noise. Co-Authored-By: Claude Opus 5.5 <[email protected]>
PartialInsertionSort took raw begin/end pointers and a limit that was always INT64_MIN, so the partial part never applied. It becomes SortByScore over a ScoredRange, a std::ranges::subrange of the scored move array, written with iterators and std::prev. The killer lookup in bad captures uses std::ranges::find with a &ScoredMove::move projection instead of find_if over data() pointers. The range is a subrange of std::array iterators rather than a std::span: GCC 15 keeps an extra pointer copy per shifted element in loops over span iterators, while array iterators compile to the same 7-instruction inner loop as before. Search is unchanged (identical nodes and best moves on 30 positions); instructions per node 11712-11716 vs 11710-11713, cycles per node 13468-13524 vs 13590-13602. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Tables keyed by Player, Piece, CastlingSide and GamePhase were plain std::arrays indexed through static_cast<size_t>(enum) and helper variables such as color_idx and piece_idx, so nothing stopped indexing a color table with a piece or a castling side with a raw 0/1. Add EnumArray<Key, Value>, an array whose operator[] takes the enum itself, with ByPlayer, ByPiece, ByCastlingSide and ByGamePhase aliases and kPlayers, kCastlingSides and kPiecesByValue lists for loops. Board state, the castling, pawn and attack tables, Zobrist keys, PSQT, piece values, the evaluation tunables, the FEN parser and the time control use them; indexing with the wrong enum or an integer no longer compiles. The TaperedEval phase pair and NNUE are unchanged. 154 of the 201 size_t casts are gone. Search is unchanged: identical nodes and best moves on 30 positions, perft startpos d5 4865609 and kiwipete d4 4085603. Machine code differs only in index arithmetic (an imul instead of a lea/sub chain for the PSQT row, one fewer saved register); instructions per node 11736 vs 11728 (+0.07%), cycles per node 13478-13631 vs 13499-13715. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Bench: 696698
Keep the constexpr pawn, piece-square and Zobrist tables indexed by their enums. Reset the typed continuation-history array in bulk and preserve the inplace move lists and 32-byte magic descriptors. Bench: 993713
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Tables keyed by
Player,Piece,CastlingSideandGamePhaseare now indexed by the enum itself, so indexing a color table with a piece, or a castling side with a raw0/1, no longer compiles. This finishes theEnumArrayidea from theenum-indexed-arraybranch.Change (
4e51dca)Chess/EnumArray.h:EnumArray<Key, Value>withoperator[](Key), iteration and==, with sizes registered next to each enum (kEnumSize<Player> = 2, …).ByPlayer,ByPiece,ByCastlingSide,ByGamePhase, and the loop listskPlayers,kCastlingSides,kPiecesByValue.static_cast<size_t>casts are gone, along with thecolor_idx/piece_idxhelper variables.TaperedEvalphase pair and NNUE are untouched.Verification (GCC 15, gate build, CPU 19, three alternating runs each)
imulinstead of alea/subchain for the PSQT row, and one fewer saved register.Stacking. Based on #65, because the FEN parser it converts comes from there. It also carries #66's two commits (
b15e5d3,8fb3af9) until #66 merges. Merge #63 → #65 and #66 → this.🤖 Generated with Claude Code